#include "userscheduledata.h"

#include <QDebug>

const QChar UserScheduleData::DEFAULT_SEPARATOR = ';';

UserScheduleData::UserScheduleData(int shiftsNumber) {
    for(int i = 0; i < shiftsNumber; ++i) {
        shifts_ << DAYOFF;
    }
}

UserScheduleData::~UserScheduleData() {
}

void UserScheduleData::setShiftType(int day, ShiftType type) {
    shifts_[day] = type;
}

ShiftType UserScheduleData::getShiftType(int day) {
    return shifts_.at(day);
}

void UserScheduleData::switchShiftType(int day) {
    switch(shifts_.at(day)) {
    case DAYOFF:
        setShiftType(day, DAILY);
        break;
    case DAILY:
        setShiftType(day, NIGHTLY);
        break;
    case NIGHTLY:
        setShiftType(day, UNAVAILABLE);
        break;
    case UNAVAILABLE:
        setShiftType(day, DAYOFF);
        break;
    }
}

void UserScheduleData::fromString(const QString &serialized, const QChar separator) {

}

QString UserScheduleData::toString(const QChar &separator) const {
    return QString();
}

/* ************************************************************************************ */

Schedule::Schedule(const QDate &date) {
    month_ = date.month();
    year_ = date.year();
    shiftsNumber_ = date.daysInMonth();
    hash_ = hash(month_, year_);

    qDebug() << "Creating schedule for date" << date.toString("MM-yyyy")
             << "with" << shiftsNumber_ << "number of shifts";
}

Schedule::~Schedule() {
    qDebug() << "Destroying schedule" << hash();
}

void Schedule::addUser(const QString &name) {
    if(!entries_.contains(name)) {
        entries_.insert(name, UserScheduleData(shiftsNumber_));
    } else {
        // TODO error
    }
}

QString Schedule::getUser(int i) const {
    return entries_.keys().at(i);
}

ShiftType Schedule::getShiftType(int user, int day) {
    QString usr = entries_.keys().at(user);
    return entries_[usr].getShiftType(day);
}

void Schedule::switchShiftType(int user, int day) {
    QString usr = entries_.keys().at(user);
    entries_[usr].switchShiftType(day);
}

QString Schedule::hash() const {
    return hash_;
}

QString Schedule::hash(int month, int year) {
    QString ret = QString::number(year);
    ret += "_";
    ret += QString::number(month);

    return ret;
}

QString Schedule::getLabel(int day) {
    QString ret = QString::number(day) + "\n";
    QDate date(year_, month_, day);
    ret += QDate::shortDayName(date.dayOfWeek());

    return ret;
}

int Schedule::getUsersNumber() const {
    return entries_.keys().size();
}

int Schedule::getShiftsNumber() const {
    return shiftsNumber_;
}

bool Schedule::userExists(const QString &user) const {
    return entries_.keys().contains(user);
}

Schedule Schedule::loadFromFile(const QDate &date) {
    QString path = "grafiki/" + hash(date.month(), date.year());
    return loadFromFile(path);
}

Schedule Schedule::loadFromFile(const QString &path) {
    return Schedule();
}
